package gui.item;

import facades.ItemFacade;
import gui.common.*;
import hitExceptions.InvalidDataException;

import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Observer;
import java.util.Observable;

import coreModel.Barcode;
import coreModel.Item;
import coreModel.Product;

/**
 * Controller class for the edit item view.
 */
public class EditItemController extends Controller 
										implements IEditItemController, Observer {
	
	private static ItemFacade itemFacade = ItemFacade.getInstance();
	private Item modifiedItem;
	private ItemData targetData;
	
	/**
	 * Constructor.
	 * 
	 * @param view Reference to edit item view
	 * @param target Item that is being edited
	 */
	public EditItemController(IView view, ItemData target) 
	{
		super(view);
		
		targetData = target;
		Item targetItem = (Item) (target.getTag());
		modifiedItem = new Item(targetItem);

		construct();
	}

	//
	// Controller overrides
	//
	
	/**
	 * Returns a reference to the view for this controller.
	 * 
	 * {@pre None}
	 * 
	 * {@post Returns a reference to the view for this controller.}
	 */
	@Override
	protected IEditItemView getView() 
	{
		return (IEditItemView)super.getView();
	}

	/**
	 * Sets the enable/disable state of all components in the controller's view.
	 * A component should be enabled only if the user is currently
	 * allowed to interact with that component.
	 * 
	 * {@pre None}
	 * 
	 * {@post The enable/disable state of all components in the controller's view
	 * have been set appropriately.}
	 */
	@Override
	protected void enableComponents() 
	{
		getView().enableDescription(false);
		getView().enableBarcode(false);
		getView().enableEntryDate(true);

		//enable iff valid date
		if (getView().getEntryDate() != null)
		{
			try
			{
				Item dateItem = new Item();
				dateItem.setEntryDate(getView().getEntryDate());
				getView().enableOK(true);
			}
			catch (InvalidDataException e)
			{
				getView().enableOK(false);
			}
		}
		else
			getView().enableOK(false);
	}

	/**
	 * Loads data into the controller's view.
	 * 
	 *  {@pre None}
	 *  
	 *  {@post The controller has loaded data into its view}
	 */
	@Override
	protected void loadValues() 
	{
		Product modP = modifiedItem.getProduct();
		String modS = modP.getDescription();
		getView().setDescription(modS);
		getView().setBarcode(modifiedItem.getBarcode().toString());
		getView().setEntryDate(modifiedItem.getEntryDate());
	}

	//
	// IEditItemController overrides
	//

	/**
	 * This method is called when any of the fields in the
	 * edit item view is changed by the user.
	 */
	@Override
	public void valuesChanged() 
	{
		enableComponents();
	}
	
	/**
	 * This method is called when the user clicks the "OK"
	 * button in the edit item view.
	 */
	@Override
	public void editItem() 
	{
		//set the item's entry date to the new date.
		try 
		{
			modifiedItem.setEntryDate(getView().getEntryDate());
		} 
		catch (InvalidDataException e) 
		{
			System.err.println("Error0 in EditItemController"
					+ "::editItem(): \n\t" + e);
		}
		//update item in model
		try 
		{
			itemFacade.editItem(modifiedItem);
		} 
		catch (InvalidDataException e) 
		{
			System.err.println("Error1 in EditItemController"
					+ "::editItem(): \n\t" + e);
		}
		
	}
	
	public void update(Observable obs, Object obj)
	{
		loadValues();
	}
	

}

